package structure;

import java.util.Arrays;

/**
 * 用数组实现队列
 */
class ArrayQueue<T> /*implements Queue<T>*/ {
  private int SIZE = 10;

  // 数组的长度不是数据真实的长度，所以需要foot尾来记录数据长度
  private Object[] arr = new Object[SIZE];
  private int foot;

  public ArrayQueue() {
  }

  public boolean isEmpty() {
    return foot == 0;
  }

  // 校验是否需要扩容
  private void checkLength(int targetIndex) {

  }

  public void add(T t) {
    //checkLength();
    Arrays.copyOfRange(arr, 0, foot++);
    arr[foot] = t;
    /*arr[foot] = t;
    if (foot++ >= arr.length) {
      Object[] nArr = new Object[arr.length + SIZE];
      for (int i = 0; i < arr.length; i++) {
        if (null != arr[i]) {
          nArr[i] = arr[i];
        } else {
          break;
        }
      }
      arr = nArr;
    }*/
  }

  public void remove() {
    Arrays.copyOfRange(arr, 1, foot--);
    /*if (!isEmpty()) {
      for (int i = 0; i < arr.length - 1; i++) {
        arr[i] = arr[i + 1];
        arr[i + 1] = null;
      }
      foot--;
    } else {
      throw new RuntimeException("队列为空");
    }*/
  }

  public int size() {
    return foot;
  }

  @SuppressWarnings("unchecked") public T get(int i) {
    if (i < foot && i >= 0) {
      return (T) arr[i];
    } else {
      throw new RuntimeException("下标为负数或越栈");
    }
  }
}